﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TemplatesPrint.DBHelper;
using TemplatesPrint.EntityHandle;
using TemplatesPrint.Entitys;
using TemplatesPrint.Tools;

namespace TemplatesPrint.Views
{
    public partial class FrmMain : UIHeaderMainFrame
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            TaskLogHandle.SaveLog("", "开启软件", "");
            LoadHeader();
            LoadWsServer();

            PublicTool.InitLoad();
            this.Text = this.Text + "-" + PublicTool.Version;
            this.WindowState = FormWindowState.Normal;
        }

        public void LoadHeader()
        {
            Header.TabControl = MainTabControl;

            //增加页面到Main
            AddPage(new FrmQryInfo(), 1001);
            AddPage(new FrmJiaMengDian(), 1002);
            AddPage(new FrmZongDaiLi(), 1003);
            AddPage(new FrmJianTui(), 1004);
            AddPage(new FrmXinLingShou(), 1005);
            AddPage(new FrmSysConfig(), 1006);


            //设置Header节点索引
            Header.CreateNode("查询", 1001);
            Header.SetNodeSymbol(Header.Nodes[0], 61451);

            Header.CreateNode("加盟店", 1002);
            Header.SetNodeSymbol(Header.Nodes[1], 61451);

            Header.CreateNode("总代理", 1003);
            Header.SetNodeSymbol(Header.Nodes[2], 61451);

            Header.CreateNode("腿带肩颈带", 1004);
            Header.SetNodeSymbol(Header.Nodes[3], 61451);

            Header.CreateNode("新零售", 1005);
            Header.SetNodeSymbol(Header.Nodes[4], 61451);

            Header.CreateNode("加盟方案", 1006);
            Header.SetNodeSymbol(Header.Nodes[5], 61451);


            //显示默认界面
            Header.SelectedIndex = 0;
        }

        void LoadWsServer()
        {
        }


        private void FrmMain_SizeChanged(object sender, EventArgs e)
        {
            //if (this.WindowState == FormWindowState.Minimized)
            //{
            //    this.Hide();
            //    this.ShowInTaskbar = false;
            //    notifyIcon1.Visible = true;
            //}
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Maximized;
            this.ShowInTaskbar = true;
            this.Activate();
        }


        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Maximized;
                this.ShowInTaskbar = true;
                this.Activate();
            }

            FrmCloseTip frmCloseTip = new FrmCloseTip();
            frmCloseTip.ShowDialog();
            if (frmCloseTip.closeType == 0)
            {
            }
            else if (frmCloseTip.closeType == 1)
            {
                this.WindowState = FormWindowState.Minimized;
            }
            else if (frmCloseTip.closeType == 2)
            {
                this.notifyIcon1.Visible = false;
              
                TaskLogHandle.SaveLog("", "关闭软件", "");
                SetOpacity();
                //System.Environment.Exit(0);
                //Application.Exit();
                //this.Close();
                this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.FrmMain_FormClosing);
                this.Close();
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {

            FrmCloseTip frmCloseTip = new FrmCloseTip();
            frmCloseTip.ShowDialog();
            if (frmCloseTip.closeType == 0)
            {
                e.Cancel = true;
            }
            else if (frmCloseTip.closeType == 1)
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
            }
            else if (frmCloseTip.closeType == 2)
            {
                TaskLogHandle.SaveLog("", "关闭软件", "");

                SetOpacity();
                //System.Environment.Exit(0);
                //Application.Exit();
                //this.Close();
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            显示ToolStripMenuItem_Click(sender, e);
        }

        /// <summary>
        /// 退出软件时，设置界面淡化退出
        /// </summary>
        void SetOpacity()
        {
            while (this.Opacity>0)
            {
                this.Opacity = this.Opacity -0.1;
                Thread.Sleep(80);
            }
        }
    }
}
